home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Utils / IE Tab / Bin / ie_tab-1.0.8-fx+fl+mz.xpi / components / nsMhtViewerFactory.js < prev   
Text File  |  2005-12-06  |  6KB  |  174 lines

  1. /**
  2.  * MHT Document Viewer Factory JS Component
  3.  * ========================================
  4.  *
  5.  *  Description: This component register .mht(.mhtml) MimeType into Mozilla Firefox
  6.  *
  7.  *  Copyright (c) 2005 yuoo2k@gmail.com
  8.  *
  9.  *  This file is part of IE Tab. http://ietab.mozdev.org
  10.  *
  11.  *  This component is free software; you can redistribute it and/or modify
  12.  *  it under the terms of the GNU General Public License as published by
  13.  *  the Free Software Foundation; either version 2 of the License, or
  14.  *  (at your option) any later version.
  15.  *
  16.  *  This component is distributed in the hope that it will be useful,
  17.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  *  GNU General Public License for more details.
  20.  *
  21.  *  You should have received a copy of the GNU General Public License
  22.  *  along with this component; if not, write to the Free Software
  23.  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24.  *
  25.  */
  26.  
  27. // Provides MHT Document Viewer Factory
  28. const mhtMimeType = "application/x-mht";
  29. const mhtViewerFactoryContractID = "@mozilla.org/content-viewer-factory/view;1?type="+mhtMimeType;
  30. const mhtViewerFactoryCID = Components.ID("{3a31c9df-535b-4d1b-9203-37cc910c807c}");
  31. const mhtViewerFactoryIID = Components.interfaces.nsIMhtViewerFactory;
  32.  
  33. var MhtViewerFactory = null;
  34.  
  35. function MhtViewerFactoryClass() { }
  36.  
  37. MhtViewerFactoryClass.prototype = {
  38.  
  39.   init: function(requestTarget) {
  40.     this.requestTarget = requestTarget;
  41.     this.addCategoryEntry();
  42.     this.registerMimeType();
  43.   },
  44.  
  45.   addCategoryEntry: function() {
  46.     var catMan = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
  47.     catMan.addCategoryEntry("Gecko-Content-Viewers", mhtMimeType, mhtViewerFactoryContractID, true, true);
  48.   },
  49.  
  50.   getMimeTypeRdfFileURL: function() {
  51.     var dirSrv = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
  52.     var rdfFile = dirSrv.get("UMimTyp", Components.interfaces.nsIFile);
  53.  
  54.     var ioSrv = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  55.     var fileHandler = ioSrv.getProtocolHandler("file").QueryInterface(Components.interfaces.nsIFileProtocolHandler);
  56.  
  57.     return fileHandler.getURLSpecFromFile(rdfFile);
  58.   },
  59.  
  60.   registerMimeType: function() {
  61.     const ncURI = "http://home.netscape.com/NC-rdf#";
  62.  
  63.     var gRDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
  64.     var gDS = gRDF.GetDataSourceBlocking(this.getMimeTypeRdfFileURL());
  65.  
  66.     var mimeSource = gRDF.GetUnicodeResource("urn:mimetype:"+mhtMimeType);
  67.  
  68.     var valueProperty = gRDF.GetUnicodeResource(ncURI+"value");
  69.     var mimeLiteral = gRDF.GetLiteral(mhtMimeType);
  70.     var currentValue = gDS.GetTarget(mimeSource, valueProperty, true);
  71.     if (currentValue) {
  72.       gDS.Change(mimeSource, valueProperty, currentValue, mimeLiteral);
  73.     } else {
  74.       gDS.Assert(mimeSource, valueProperty, mimeLiteral, true);
  75.     }
  76.  
  77.     var fileExtsProperty = gRDF.GetUnicodeResource(ncURI+"fileExtensions");
  78.     gDS.Assert(mimeSource, fileExtsProperty, gRDF.GetLiteral("mht"), true);
  79.     gDS.Assert(mimeSource, fileExtsProperty, gRDF.GetLiteral("mhtml"), true);
  80.  
  81.     // add the mime type to the MIME types seq
  82.     var container = Components.classes["@mozilla.org/rdf/container;1"].createInstance();
  83.     if (container) {
  84.       container = container.QueryInterface(Components.interfaces.nsIRDFContainer);
  85.       if (container) {
  86.         var containerRes = gRDF.GetUnicodeResource("urn:mimetypes:root");
  87.         container.Init(gDS, containerRes);
  88.         if (container.IndexOf(mimeSource) == -1) container.AppendElement(mimeSource);
  89.       }
  90.     }
  91.  
  92.     gDS.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource).Flush();
  93.   },
  94.  
  95.   createInstance: function(command, channel, loadGroup, contentType, container, extraInfo, docListenerResult) {
  96.     this.requestTarget.loadIeTab(channel.URI.spec); //loadMHT()
  97.     docListenerResult = null;
  98.     return null;
  99.   },
  100.  
  101.   createBlankDocument: function(loadGroup) {
  102.     return null;
  103.   },
  104.  
  105.   createInstanceForDocument: function(container, document, command) {
  106.     return null;
  107.   },
  108.  
  109.   QueryInterface: function(iid) {
  110.     if (!iid.equals(Components.interfaces.nsIDocumentLoaderFactory) &&
  111.         !iid.equals(mhtViewerFactoryIID) &&
  112.         !iid.equals(Components.interfaces.nsISupports)) {
  113.       throw Components.results.NS_ERROR_NO_INTERFACE;
  114.     }
  115.     return this;
  116.   }
  117. };
  118.  
  119. var MhtViewerFactoryFactory = new Object();
  120.  
  121. MhtViewerFactoryFactory.createInstance = function (outer, iid) {
  122.   if (outer != null) {
  123.     throw Components.results.NS_ERROR_NO_AGGREGATION;
  124.   }
  125.  
  126.   if (!iid.equals(Components.interfaces.nsIDocumentLoaderFactory) &&
  127.       !iid.equals(mhtViewerFactoryIID) &&
  128.       !iid.equals(Components.interfaces.nsISupports)) {
  129.     throw Components.results.NS_ERROR_NO_INTERFACE;
  130.   }
  131.  
  132.   if (MhtViewerFactory == null) {
  133.     MhtViewerFactory = new MhtViewerFactoryClass();
  134.   }
  135.  
  136.   return MhtViewerFactory.QueryInterface(iid);
  137. };
  138.  
  139.  
  140. /**
  141.  * XPCOM component registration
  142.  */
  143. var MhtViewerFactoryModule = new Object();
  144.  
  145. MhtViewerFactoryModule.registerSelf = function (compMgr, fileSpec, location, type) {
  146.   compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  147.   compMgr.registerFactoryLocation(mhtViewerFactoryCID,
  148.                                   "MHT Document Viewer Factory JS Component",
  149.                                   mhtViewerFactoryContractID,
  150.                                   fileSpec,
  151.                                   location,
  152.                                   type);
  153. };
  154.  
  155. MhtViewerFactoryModule.getClassObject = function(compMgr, cid, iid) {
  156.   if (!cid.equals(mhtViewerFactoryCID)) {
  157.     throw Components.results.NS_ERROR_NO_INTERFACE;
  158.   }
  159.  
  160.   if (!iid.equals(Components.interfaces.nsIFactory)) {
  161.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  162.   }
  163.  
  164.   return MhtViewerFactoryFactory;
  165. };
  166.  
  167. MhtViewerFactoryModule.canUnload = function (compMgr) {
  168.   return true;
  169. };
  170.  
  171. function NSGetModule(compMgr, fileSpec) {
  172.   return MhtViewerFactoryModule;
  173. };
  174.